package com.jinbo.footmessage.serverInf;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jinbo.footmessage.R;
import com.jinbo.footmessage.httpbean.ProjectBean;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by jinbo on 2015/8/19.
 */
public class ShopItemListViewAdapter extends BaseAdapter {

    TextView total_number, total_price;
    List<ProjectBean> projects = new ArrayList<ProjectBean>();
    List<ProjectBean> all_projects;
    private Callbacks mCallBacks;

    public interface Callbacks {
        public void change(List<ProjectBean> projects);
    }

    static class ViewHolder {
        //LinearLayout ll;
        TextView deleteTv;
        TextView nameTv;
        TextView priceTv;


    }

    //ArrayList<ProjectsBean> projects;
    private Activity context;
    // int sum;
    // private View header_view;

    public ShopItemListViewAdapter(Activity context, ProjectItemListViewAdapter adapter, View headerView, View view) {//ArrayList<ProjectsBean> datalist) {
        this.context = context;
        all_projects = adapter.getProjects();
        if (all_projects != null) {
            for (int i = 0; i < all_projects.size(); i++)
                if (all_projects.get(i).flag)
                    projects.add(all_projects.get(i));
        }
        mCallBacks = (Callbacks) adapter;
        //View headerView = LayoutInflater.from(context).inflate(R.layout.list_shop_header, null);//这样传进来获取组件是不行的
        //View view = LayoutInflater.from(context).inflate(R.layout.dialog_shop, null);//这样传进来获取组件是不行的
        this.total_number = (TextView) headerView.findViewById(R.id.total_num);
        this.total_price = (TextView) view.findViewById(R.id.total_price);
        this.total_number.setText(getCount() + "");
        updatePrice();
    }

   /* void setprojects(List<ProjectBean> projects) {
        this.projects = projects;
    }*/
   List<ProjectBean> getProjects()
   {
       return  projects;
   }
    void empty() {
        //  sum=0;
        total_number.setText(0 + "");
        for(int i=0;i<projects.size();i++)
            updateAllProjects(projects.get(i));
        projects.clear();
        notifyDataSetChanged();
        updatePrice();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub*//*
       // int sum = 0;
        if (projects != null) {
            //for (int i = 0; i < projects.size(); i++)
              //  if (projects.get(i).flag)
               //     sum++;
             return projects.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        if (projects != null) {
            return projects.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_shop_item, null);
            holder = new ViewHolder();
            holder.deleteTv = (TextView) convertView.findViewById(R.id.shop_delete);
            holder.nameTv = (TextView) convertView.findViewById(R.id.shop_name);
            holder.priceTv = (TextView) convertView.findViewById(R.id.shop_price);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //if(projects.get(position).flag) {
        holder.nameTv.setText(projects.get(position).name);
        holder.priceTv.setText(projects.get(position).price + "元");
        //}
        holder.deleteTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateAllProjects(projects.get(position));
                projects.remove(position);
                notifyDataSetChanged();
                total_number.setText("" + (Integer.valueOf(total_number.getText().toString()) - 1));
                updatePrice();

            }
        });
        return convertView;
    }


    void updatePrice() {
        int sum_price = 0;
        if (projects != null)
            for (int i = 0; i < projects.size(); i++) {
               // if (projects.get(i).flag)
                    sum_price = sum_price + Integer.valueOf(projects.get(i).price);
            }
        total_price.setText(sum_price + "");
    }

    void updateAllProjects(ProjectBean bean) {
            for (int j = 0; j < all_projects.size(); j++)
                if (bean.id == all_projects.get(j).id)
                    all_projects.get(j).flag = false;
        mCallBacks.change(all_projects);
    }
}
